package com.beiye.drivertransport.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.bean.CarBean;

import java.util.List;

/**
 * Created by ky on 2018/4/27.
 */


public class CarApt extends CommonAdapter<CarBean.RowsBean> {
    private final List<CarBean.RowsBean> mList;
    private Context context;
    // 标记用户当前选择
    private int index = -1;


    public CarApt(Context context, List<CarBean.RowsBean> mList, int itemLayoutId) {
        super(context, mList, itemLayoutId);
        this.context = context;
        this.mList = mList;
    }

    @Override
    public void convert(ViewHolder holder, CarBean.RowsBean item, final int position) {
        RadioButton tv_car = holder.getView(R.id.tv_car);
         tv_car.setText(mList.get(position).getPlateNo());
        tv_car.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                if (isChecked) {
//                    Toast.makeText(context, mList.get(position).getPlateNo(),
//                            Toast.LENGTH_LONG).show();
                    String vid = mList.get(position).getVid();
                    String plateNo = mList.get(position).getPlateNo();
                    SharedPreferences sp =context.getSharedPreferences("MessageEvent",
                            Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("vid",vid);
                    editor.putString("plateNo",plateNo);

                    editor.commit();
                    index = position;
                    notifyDataSetChanged();
                }
            }
        });
        if (index == position) {// 选中的条目和当前的条目是否相等
            tv_car.setChecked(true);
            tv_car.setBackgroundResource(R.drawable.carbtnpress);  //选中项背景
            tv_car.setTextColor(context.getResources().getColor(R.color.white_name) );
        } else {
            tv_car.setChecked(false);
            tv_car.setBackgroundResource(R.drawable.carbtn);  //其他项背景
            tv_car.setTextColor(context.getResources().getColor(R.color.project_blue) );
        }


    }




}